<?php

namespace app\index\controller;

use app\index\model\Departments;
use app\index\model\Employees;
use think\Request;

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function testHello(Request $request){
        // dump($request->url());
        // dump($request->baseUrl());
        // dump($request->path());
        // dump($request->query());
        
        // dump(url("/index/index/testHello"));

        dump($request->module());
        dump($request->controller());
        dump($request->action(true));



    }

    //题目1：查询所有部门的名称以及其对应的员工数量（包括没有员工的部门）。
    public function demo01(Departments $departments_model)
    {
        $departemnet_list = $departments_model->selectDepartmentNameWithEmpCount();
        return json($departemnet_list);
    }

    //题目2：找出所有没有分配到任何部门的员工的信息。
    public function demo02(Employees $employees_model)
    {
        $employye_list = $employees_model->seletEmployeeNoDepartment();
        return json($employye_list);
    }

    //题目3：计算每个部门的平均薪水，并按照平均薪水降序排列。
    public function demo03(Departments $departments_model)
    {
        $departemnet_list = $departments_model->selectDepartmentAvgSalary();
        return json($departemnet_list);
    }

    //题目4：查询薪水最高的员工的信息。
    public function demo04(Employees $employees_model)
    {
        $employee = $employees_model->selectMaxSalary();
        return json($employee);
    }

    //题目5：列出所有员工的姓氏和名字，按照姓氏字母顺序排序。
    public function demo05(Employees $employees_model)
    {
        $employye_list = $employees_model->selectEmployyeeAndOrder();
        return json($employye_list);
    }

    //题目6：找出薪水高于平均薪水且所在部门名称以“销售”结尾的员工信息。
    public function demo06(Employees $employees_model)
    {
        $employye_list = $employees_model->selectEmpAndMoreAvgSalary();
        return json($employye_list);
    }

    //题目7：查询所有员工的薪水范围（最低薪水到最高薪水）。
    public function demo07(Employees $employees_model)
    {
        $emp_list = $employees_model->selectEmpSalaryRange();
        return json($emp_list);
    }

    //题目8：创建一个视图，显示每个部门的总薪水以及该部门员工数量。
    public function demo08()
    {
        //................
    }

    //题目9：插入一个新部门到departments表中，并分配一个员工到这个新部门。
    public function demo09(Departments $departments_model, Employees $employees_model)
    {
        $department_id = $departments_model->addDepartmentDataAndRetId([
            "department_name" => "运营部"
        ]);
        if ($department_id != false) {
            $employee_data = [
                "first_name" => "三",
                "last_name" => "李",
                "department_id" => $department_id,
                "salary" => 4500
            ];
            $add_result = $employees_model->addEmployeeData($employee_data);
            if ($add_result != false) {
                return json("插入成功!");
            } else {
                return json("err:002");
            }
        } else {
            return json("err:001");
        }
    }

    //题目10：更新一个特定部门的名称，并相应更新所有相关员工的department_id。
    public function demo10(Departments $departments_model){
        $upd_result=$departments_model->updDepartmentName([
            "department_name" => "人力资源部"
        ],["department_id"=>1]);
        if ($upd_result != false) {
            return json("更新成功!");
        } else {
            return json("err:001");
        }
    }
}
